package com.itany.interceptor;

import com.alibaba.fastjson.JSON;
import com.itany.ApplicationContextHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

public class LiveInterceptor implements HandlerInterceptor {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) {
        StringRedisTemplate template = ApplicationContextHolder.getBean(StringRedisTemplate.class);
        Cookie[] cks = request.getCookies();
        if (null != cks) {
            for (int i = 0; i < cks.length; i++) {
                Cookie ck = cks[i];
                if ("TT_TOKEN".equals(ck.getName())) {
                    String token = ck.getValue();
                    String str = template.opsForValue().get("USER_TOKEN::" + token);
                    System.out.println("==============" + str);
                    template.opsForValue().set("USER_TOKEN::" + token, str, 1800, TimeUnit.SECONDS);
                }
            }
        }
    }
}
